
$('.tou').load('head.html')
$('.wei').load('footer.html')
function que(name) {
    var search = location.search.replace('?', '');
    search = search.split('&')
    search = search.map((value) =>
        ({
            key: value.split('=')[0],
            value: value.split('=')[1]
        }))
    var data = search.filter(({ key }) => key === name)[0]
    return data ? data.value : ''
}
var name1 = que('id')//用户名
var uid = que('uid')//商品名
var gid = que("gid") //商品id
var img = que('img') //商品img
var price = que('price') //商品价格
var count = 1//数量

uid = decodeURIComponent(uid)


$.ajax({
    type: "GET",
    url: "http://localhost/lian/pubg/php/list2.php",
    data: { username: 333 },
    success: function (data) {
        var data2 = JSON.parse(data)
        console.log(data2);
        var html = ``
        $.each(data2.data2, function (index, { id, img, uid, price, count }) {


            html += ` <div class="mycart-item" >
                        <div class="mycart-item-tb">
                            <div class="mycart-item-row cartlist clearfix" biz="pubg" mallid="8128" cartid="59127"
                                price="51800" name="周年典藏礼盒标准版"
                                pic="https://game.gtimg.cn/images/zb/x5/uploadImg/goods/201902/20190215173858_68419.jpg"
                                type="0">
                                <div class="fl sp-1">
                                    <span class="goodscheck goods1 input-check"><i></i></span>
                                </div>
                                <div class="fl sp-2">
                                    <div class="mycart-item-img">
                                        <a href="./detail.shtml?id=8128" target="_blank"><img
                                                src="${img}"
                                                alt="商品名称"></a>
                                    </div>
                                </div>
                                <div class="fl sp-3">
                                    <p class="mycart-item-name">
                                        <a href="./detail.shtml?id=8128" target="_blank">${uid}</a>
                                    </p>
                                    <div class="mycart-item-tag">051800
                                    </div>
                                </div>
                                <div class="fl sp-4">
                                    <div class="mycart-item-sel disabled">
                                        <p>礼盒分类：典藏礼盒标准版</p>
                                        <!--<i>修改</i>-->
                                    </div>
                                </div>
                                <div class="fl sp-5">
                                    <p class="mycart-item-per">
                                        <s>${price * 2}.00 元</s>
                                        ${price}.00 元
                                    </p>
                                </div>
                                <div class="fl sp-6">
                                    <div class="mycart-item-count" id-u="${id}" id-p="${price}">
                                        <span class="num-minus btn_cart_list_buy_min">-</span>
                                        <input type="text" class="inpt_cart_list_buy_num" value="${count}" stock_num="67">
                                        <span class="num-plus btn_cart_list_buy_plus">+</span>
                                    </div>
                                </div>
                                <div class="fl sp-7">
                                    <p class="mycart-item-price">
                                        ${price * count}.00 元
                                    </p>
                                </div>
                                <div class="fl sp-8">
                                    <div class="mycart-item-tg" id-n="${id}" >
                                        <a class="btn-del" href="javascript:;">删除</a>
                                        <a class="btn-fav" href="javascript:;">移入收藏夹</a>
                                    </div>
                                </div>
                            </div>
                        </div>`
        })

        $('.mycart-list').html(html)
        ddd()
        $(document).on('click', function (e) {
            var target = $(e.target);
            var n = target.prev().val()
            //点击减
            if (target.hasClass("num-plus")) {
                var id2 = $(target).parent().attr("id-u")
                var price1 = $(target).parent().attr("id-p")
                target.prev().val(++n)
                $(target).parents(".mycart-item-tb").find(".mycart-item-price").html(n * price1 + ".00 元")

                yun(id2, n)
                ddd()
            }
            //点击添加
            if (target.hasClass("num-minus")) {
                var id2 = $(target).parent().attr("id-u")
                n = target.next().val()
                n--
                if (n >= 1) {
                    target.next().val(n)
                    var price1 = $(target).parent().attr("id-p")

                    $(target).parents(".mycart-item-tb").find(".mycart-item-price").html(n * price1 + ".00 元")

                    yun(id2, n)
                }
                ddd()

            }
            //点击删除
            if (target.hasClass("btn-del")) {
                var id2 = $(target).parent().attr("id-n")

                $(target).parents(".mycart-item-tb").remove()

                console.log(66);

                dell(id2)
            }
            if (target.hasClass("mycart-tb-btn")) {
                console.log(666);



            }

        })

    }
})
//修改count值
function yun(id, n) {
    $.ajax({
        type: "GET",
        url: "http://localhost/lian/pubg/php/gai.php",
        data: { id, count: n },
        success: function (data) {

        }

    })
}

//删除
function dell(id) {
    $.ajax({
        type: "GET",
        url: "http://localhost/lian/pubg/php/delete.php",
        data: { id },
        success: function (data) {
            var data2 = JSON.parse(data)
        }
    })

}

//选项卡
function check() {


}
//全选
$(document).on("click", ".cartcheck", function () {
    console.log(666);


    active(!$(this).hasClass('checked'), [this, $('.goodscheck')])
    ddd()
})
//控制全选

function checkAll() {
    if ($(".checked").length === $('.goods').length) {
        active(true, [$('.cartcheck')])

    } else {
        active(false, [$('.cartcheck')])
        console.log(11);

    }
}
//单选
$(document).on('click', ".goodscheck", function (e) {

    var target = $(e.target);

    active(!$(this).hasClass("checked"), [this])
    if ($(".sp-1").find(".checked").length === $('.goods1').length) {
        active(true, [$('.cartcheck')])

    } else {
        active(false, [$('.cartcheck')])
        console.log(11);

    }
    ddd()

})
function active(status, eles) { // status为true代表激活

    $.each(eles, function (index, item) {
        if (status) {
            $(item).addClass('checked')
        } else {
            $(item).removeClass('checked')

        }
    })
}
//总价
function ddd() {
    //总价格
    var total = 0
    //单价数量
    var mun = 0
    $.each($(".mycart-item-price"), function (index, item) {
        var kk = $(item).parent().siblings(".sp-1").find(".goods1").hasClass("checked")

        var inp = $(item).parent().siblings(".sp-6").find("input").val()
        if (kk) {
            var dd = $(item).html()
            mun += inp * 1

            total += 1 * dd.replace('元', '');
        }

    })

    $('.money').html(`${total}.00 元`)
    $('.total').html(`${mun}`)
    //结算提交
    $(".btnsuan").on('click', function () {
         var fola = $('.total').html()
         
         //判断是否可以提交结算
        if (fola*1) {
            var obj1 = []
            $.each($(".checked"), function (index, item) {

                if ($(item).hasClass("goods1")) {
                    obj1.push({
                        img: $(item).parent().siblings(".sp-2").find("img").attr("src"),
                        name: $(item).parent().siblings(".sp-3").find("a").html(),
                        price: $(item).parent().siblings(".sp-5").find("s").html().replace('元', '') * 1 / 2,
                        count: mun,
                    })

                }
            })
            data = JSON.stringify(obj1);
            localStorage.setItem("data1", data);
            location.href = `./sun.html`
           
        }else{
            layui.use(['layer', 'form'], function () {
                layer.alert('一件商品不选，结算个毛线结算')
            });
        }
    })





}

